
const BASEURL = 'http://localhost:3000/api'
export default function request({ url, method = "GET",data,isAuth=true}) {
  return new Promise((resolve, reject) => {

    // 处理请求头
    const header = {}
    if (isAuth) { // 是否需要授权
      const my_token = wx.getStorageSync('my_token')
      if (my_token) {
        header.Authorization = my_token
      } else {
        wx.navigateTo({
          url: '/pages/login/index',
        })
        return
      }
    }
    wx.showLoading({
      title: '加载中'
    })
    
    wx.request({
      url: `${BASEURL}${url}`,
      method,
      data,
      header,
      success: res => {
        if(res.data.status===0){
          resolve(res.data)
        }else{
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }, fail: err => {
        reject(err)
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  })
}